package com.kaisir.demo.flow.controller;

import com.kaisir.demo.flow.bean.SnakerEngineFacets;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import java.util.Map;

@Controller
@SessionAttributes({"app", "user"})
public class ApplyController {

    @Autowired
    SnakerEngineFacets engineFacets;

    @RequestMapping(value = "applyForm", method = RequestMethod.POST)
    public String applyFormPost(@ModelAttribute String app, @ModelAttribute String user, String processId, String type, String helper) {
        //这里是跟业务流程耦合的地方 >_<" 考虑干预类？ 把Type类型放到流程实例，此处还需放入当前用户相关的其他用户!!
        Map<String, Object> attrMap = new HashedMap();
        attrMap.put("type", type);
        attrMap.put("apply.creator", user); //指定发起人
        attrMap.put("apply.helper", helper);
        engineFacets.startAndExecute(processId, user, attrMap);
        return "redirect:index";
    }

    @RequestMapping(value = "applyForm", method = RequestMethod.GET)
    public String applyFormGet(Model model, String processId) {
        model.addAttribute("processId", processId);
        return "logic/applyForm";
    }
}
